package service

import (
	"errors"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: Register
//@description: 添加category
//@param: u model.SysCategory
//@return: err error, userInter model.SysCategory

func AddCategory(u model.SysCategory) (err error, categoryInfo model.SysCategory) {
	var category model.SysCategory
	if !errors.Is(global.GVA_DB.Where("categoryname = ?", u.CategoryName).First(&category).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
		return errors.New("用户名已注册"), categoryInfo
	}
	// 否则 附加uuid 密码md5简单加密 注册
	//u.Password = utils.MD5V([]byte(u.Password))
	u.UUID = uuid.NewV4()
	err = global.GVA_DB.Create(&u).Error
	return err, u
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCategory
//@description: 删除用户
//@param: id float64
//@return: err error

func DeleteCategory(id float64) (err error) {
	var category model.SysCategory
	err = global.GVA_DB.Where("id = ?", id).Delete(&category).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserInfoList
//@description: 分页获取数据
//@param: info request.PageInfo
//@return: err error, list interface{}, total int64

func GetCategoryList(info request.PageInfo,search model.SysCategory) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&model.SysUser{})
	var userList []model.SysUser
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userList).Error
	return err, userList, total
}